package com.gitee.mrj.aiboot.core.sf.generators.ui;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.SFGrid;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFGridGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFGrid grid = AnnotatedElementUtils.findMergedAnnotation(field, SFGrid.class);
        generate(node, grid);
    }

    @Override
    public void generate(UIForm node, Class clazz) {
        SFGrid grid = AnnotatedElementUtils.findMergedAnnotation(clazz, SFGrid.class);
        generate(node, grid);
    }

    private void generate(UIForm node, SFGrid grid) {
        ObjectNode ui = node.getUi();
        ObjectNode uiGrid = putUiGrid(ui);

        putInt(uiGrid, "gutter", grid.gutter());
        putInt(uiGrid, "offset", grid.offset());
        if (!grid.value().equals(SFGrid.Grid.NULL)) {
            uiGrid.put("xxl", grid.value().getXxl());
            uiGrid.put("xl", grid.value().getXl());
            uiGrid.put("xs", grid.value().getXs());
            uiGrid.put("sm", grid.value().getSm());
            uiGrid.put("md", grid.value().getMd());
            uiGrid.put("lg", grid.value().getLg());
        } else {
            putInt(uiGrid, "xxl", grid.xxl());
            putInt(uiGrid, "xl", grid.xl());
            putInt(uiGrid, "xs", grid.xs());
            putInt(uiGrid, "sm", grid.sm());
            putInt(uiGrid, "md", grid.md());
            putInt(uiGrid, "lg", grid.lg());
        }
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFGrid.class};
    }
}
